package tests;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import parsing.CritterParser;
import parsing.Parser;
import parsing.productions.Program;

public class ExceptionsInMutationTest {

	private static Reader input;
	private static Writer output;
	private static Parser parser;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		parser = new CritterParser();
	}
	
	@Before
	public void setUp(){
		try{
			input = new FileReader(new File("testInput.txt"));
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}
	
	/**
	 * Tests that a program can be mutated many times over without errors occurring
	 * will cause exceptions upon failure
	 */
	@Test
	public void manyMutations(){
		Program program = parser.parse(input);
			for(int i = 0; i < 10000; i++){
				Program.mutate(program);
				StringBuffer sb = new StringBuffer();
				program.prettyPrint(sb);
				try {
					output = new PrintWriter(new File("testOutput.txt"));
					output.write(sb.toString());
					output.close();
				} catch (IOException e) {
					System.out.println(e.getMessage());
				}
			}
		
	}
}
